﻿//********************************************************************************//
//创建日期：<创建日期，2016-08-31>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：报表管理
//********************************************************************************//

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using Web.Class;
using YXYLCommon;

namespace Web.Pages.YongShui.BaoBiaoGL
{
    public partial class BaoBiaoDY : BasePage
    {
        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
            }
            else
            {
                ReloadCSControls();
            }
            LoadData();
            Display();
        }

        #endregion


        protected void btnLoadPara_Click(object sender, EventArgs e)
        {
            if (tree1.FocusedNode.Level == 2)
            {
                //获取报表参数列表
                baobiaoid = int.Parse(tree1.FocusedNode.Key);
                csList = dalCS.GetByBaoBiaoId(baobiaoid);
                ReloadCSControls();
                LoadPrintOptions();
                tabPZ.Visible = true;
            }
            else
            {
                tabPZ.Visible = false;
            }
        }

        /// <summary>
        ///     重新加载参数控件
        /// </summary>
        private void ReloadCSControls()
        {
            if (baobiaoid == -1)
                return;
            tabCS.Rows.Clear();
            //重新加载参数控件
            var controlnames = new List<string>();
            var canshulxs = new List<string>();
            for (var i = 0; i < csList.Count; i++)
            {
                var row = new TableRow();
                row.Cells.AddRange(new[] {new TableCell(), new TableCell()});
                row.Cells[0].HorizontalAlign = HorizontalAlign.Right;
                row.Cells[0].Text = csList[i].S_CANSHUMC + "：";
                if (csList[i].I_CANSHULX.Equals(1)) //筛选
                {
                    var ic = new ASPxComboBox();
                    ic.ID = csList[i].S_CANSHU;
                    ic.ClientInstanceName = csList[i].S_CANSHU;
                    ic.DataSource = dalCS.GetDDLList(baobiaoid,
                        string.IsNullOrEmpty(csList[i].S_SHUJUY) ? 0 : int.Parse(csList[i].S_SHUJUY));
                    ic.TextField = "TEXT";
                    ic.ValueField = "VALUE";
                    ic.DataBind();
                    row.Cells[1].Controls.Add(ic);
                }
                else
                {
                    var ic = new ASPxTextBox();
                    ic.ID = csList[i].S_CANSHU;
                    ic.ClientInstanceName = csList[i].S_CANSHU;
                    ic.AutoCompleteType = AutoCompleteType.None;
                    row.Cells[1].Controls.Add(ic);
                }
                tabCS.Rows.AddAt(i, row);
                controlnames.Add(csList[i].S_CANSHU);
                canshulxs.Add(csList[i].I_CANSHULX.Equals(1) ? "cmb" : "txt");
            }
            hdCS.Set("controlnames", string.Join(",", controlnames));
            hdCS.Set("canshulxs", string.Join(",", canshulxs));
            hdCS.Set("baobiaoid", baobiaoid);
        }

        /// <summary>
        ///     加载模版打印参数
        /// </summary>
        private void LoadPrintOptions()
        {
            baobiaombcs = dalMBCS.GetByBaoBiaoAndZhuJi(baobiaoid, null, LoginInfo.CurrentUser.UserID.ToString());
            if (baobiaombcs.IsExists)
            {
                txtX.Text = baobiaombcs.I_X.ToString();
                txtY.Text = baobiaombcs.I_Y.ToString();
                txtCopies.Text = baobiaombcs.I_COPIES.ToString();
                txtPrintName.Text = baobiaombcs.S_PRINTERNAME;
                //当前取得的打印参数是当前登录人保存的,则为更新,否则需添加.
                if (baobiaombcs.S_ZHUJIIP == LoginInfo.CurrentUser.UserID.ToString())
                    baobiaombcs.Operate = eModelOperate.Update;
                else
                    baobiaombcs.Operate = eModelOperate.Add;
            }
            else
            {
                baobiaombcs = new BaoBiaoMBCS();
                baobiaombcs.Operate = eModelOperate.Add;
                txtX.Text = "0";
                txtY.Text = "0";
                txtCopies.Text = "1";
                txtPrintName.Text = "";
            }
        }

        //public string GetWebClientIp()
        //{
        //    string userIP = "未获取用户IP";

        //    try
        //    {
        //        if (System.Web.HttpContext.Current == null
        //    || System.Web.HttpContext.Current.Request == null
        //    || System.Web.HttpContext.Current.Request.ServerVariables == null)
        //            return "";

        //        string CustomerIP = "";

        //        //CDN加速后取到的IP   
        //        CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
        //        if (!string.IsNullOrEmpty(CustomerIP))
        //        {
        //            return CustomerIP;
        //        }

        //        CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];


        //        if (!String.IsNullOrEmpty(CustomerIP))
        //            return CustomerIP;

        //        if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
        //        {
        //            CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        //            if (CustomerIP == null)
        //                CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        //        }
        //        else
        //        {
        //            CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

        //        }

        //        if (string.Compare(CustomerIP, "unknown", true) == 0)
        //            return System.Web.HttpContext.Current.Request.UserHostAddress;
        //        return CustomerIP;
        //    }
        //    catch { }

        //    return userIP;
        //}  
        protected void btnPreview_Click(object sender, EventArgs e)
        {
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
        }

        /// <summary>
        ///     响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, CallbackEventArgs e)
        {
            var cr = new CallbackResult(e.Parameter);
            if (e.Parameter == "SaveOptions")
            {
                if (baobiaombcs.Operate == eModelOperate.Add)
                {
                    baobiaombcs.S_ZHUJIIP = LoginInfo.CurrentUser.UserID.ToString();
                    baobiaombcs.I_BAOBIAOID = DbInt.Parse(baobiaoid);
                }
                baobiaombcs.I_X = DbInt.Parse(txtX.Text);
                baobiaombcs.I_Y = DbInt.Parse(txtY.Text);
                baobiaombcs.I_COPIES = DbInt.Parse(txtCopies.Text);
                baobiaombcs.S_PRINTERNAME = txtPrintName.Text;
                var rs = dalMBCS.Save(baobiaombcs);
                cr.IsSuccess = rs.IsSuccess;
                cr.IsAlertMsg = true;
                cr.Msg = rs.ReturnMsg;
                e.Result = cr.ToString();
            }
        }

        #region 属性

        private List<BaoBiaoCS> csList
        {
            get
            {
                if (ViewState["csList"] == null)
                    ViewState["csList"] = new List<BaoBiaoCS>();
                return (List<BaoBiaoCS>) ViewState["csList"];
            }
            set { ViewState["csList"] = value; }
        }

        /// <summary>
        ///     记录当前操作的报表ID
        /// </summary>
        private int baobiaoid
        {
            get
            {
                if (ViewState["baobiaoid"] == null)
                    return -1;
                return Convert.ToInt32(ViewState["baobiaoid"]);
            }
            set { ViewState["baobiaoid"] = value; }
        }

        /// <summary>
        ///     记录当前操作的模版
        /// </summary>
        private BaoBiaoMBCS baobiaombcs
        {
            get
            {
                if (ViewState["baobiaombcs"] == null)
                    return null;
                return (BaoBiaoMBCS) ViewState["baobiaombcs"];
            }
            set { ViewState["baobiaombcs"] = value; }
        }

        #endregion

        #region 变量

        /// <summary>
        ///     数据访问操作类
        /// </summary>
        private readonly SpHelper _spHelper = new SpHelper();

        /// <summary>
        ///     参数DAL
        /// </summary>
        private readonly DALBaoBiaoCS dalCS = new DALBaoBiaoCS();

        /// <summary>
        ///     模版打印参数DAL
        /// </summary>
        private readonly DALBaoBiaoMBCS dalMBCS = new DALBaoBiaoMBCS();

        #endregion

        #region 方法

        /// <summary>
        ///     初始化控件
        /// </summary>
        private void InitControl()
        {
        }

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
        }

        /// <summary>
        ///     数据加载
        /// </summary>
        private void LoadData()
        {
            tree1.DataSource = _spHelper.P_YSBB_HUOQULB(BusinessTool.WType, 1).Tables[0];
            tree1.DataBind();
        }

        /// <summary>
        ///     用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            if (tree1.FocusedNode == null || tree1.FocusedNode.Level == 1)
                tabPZ.Visible = false;
        }

        /// <summary>
        ///     设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
        }


        private void PrepareBaoBiao(eReportOpenType type)
        {
            //获取参数值
            for (var i = 0; i < csList.Count; i++)
            {
                Control c = null;
                for (var j = 0; j < tabCS.Rows.Count; j++)
                {
                    c = tabCS.Rows[j].Cells[1].FindControl(csList[i].S_CANSHU);
                    if (c != null)
                        break;
                }
                if (csList[i].S_CANSHUZLX.Equals(1))
                    csList[i].S_CANSHUZ = ((ASPxComboBox) c).Value.ToString();
                else
                    csList[i].S_CANSHUZ = ((ASPxTextBox) c).Text;
            }
        }

        #endregion
    }
}